﻿Public Class ClsUserSettingsFile
    Inherits ClsFile
    'represents user settings file

#Region "Attributes"
#Region "Private attributes"
    Private MoDsUserSettings As DsUserSettings = New DsUserSettings 'user settings DS
#End Region
#End Region
#Region "Constructors"
    Sub New()
        MyBase.New()
    End Sub

    Sub New(ByVal IsPath As String)
        MyBase.New(IsPath)
    End Sub
#End Region
#Region "Properties"
#Region "Public attributes"
    Public Property DsUserSettings() As DsUserSettings
        Get
            Return MoDsUserSettings
        End Get
        Set(ByVal IoValue As DsUserSettings)
            MoDsUserSettings = IoValue
        End Set
    End Property
#End Region
#End Region
#Region "Functions"
#Region "Open file&DS, Save file&DS, Close file&DS"
    'opens file
    Public Overrides Function OpenFile() As GeActionResult
        Try
            If MyBase.OpenFile = GeActionResult.kkOk Then
                Try
                    MoDsUserSettings.ReadXml(MsPath, XmlReadMode.IgnoreSchema)
                    MeStateOfFile = GeFileState.kkOpened
                    Return GeActionResult.kkOk
                Catch ex As Exception
                    'probably file is empty = without DS in XML file
                    CreateDefaultUserSettings()
                    MeStateOfFile = GeFileState.kkOpened
                    Return GeActionResult.kkOk
                End Try
            Else
                'file is not opened but I need to fill DS for next using in apllication
                CreateDefaultUserSettings()
                Return GeActionResult.kkOk
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'saves file
    Public Function SaveFile() As GeActionResult
        Try
            MoDsUserSettings.WriteXml(MsPath, XmlWriteMode.WriteSchema)

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Manipulating with DS"
#Region "INSERT"
    'inserts new dock panels position to table UserSettings
    Private Function InsertNewDockPanelsPosition() As GeActionResult
        Try
            Dim LoRow As DataRow

            LoRow = MoDsUserSettings.Tables("UserSettings").NewRow()
            LoRow("DockPanelsPosition") = ""

            MoDsUserSettings.Tables("UserSettings").Rows.Add(LoRow)

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "UPDATE"
    'updates dock panels position in table UserSettings
    Public Function UpdateDockPanelsPosition(ByVal IsValue As String) As GeActionResult
        Try
            Dim LoRows() As DataRow = MoDsUserSettings.Tables("UserSettings").Select()
            If LoRows.Length = 1 Then
                LoRows(0)("DockPanelsPosition") = IsValue
            Else
                Throw New Exception
            End If

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "DELETE"

#End Region
#Region "SELECT"
    'gets dock panels position
    Public Function GetDockPanelsPosition() As String
        Try
            Dim LoRows() As DataRow = MoDsUserSettings.Tables("UserSettings").Select()
            If LoRows.Length = 1 Then
                Return LoRows(0).Item("DockPanelsPosition")
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return "" 'default
        End Try
    End Function
#End Region
#End Region
#Region "Other Functions"
    'creates default settings
    Private Function CreateDefaultUserSettings() As GeActionResult
        Return InsertNewDockPanelsPosition()
    End Function
#End Region
#End Region

End Class
